package com.asset.dispoal.file.casedispose.strategy.command;

import com.alibaba.excel.EasyExcel;
import com.asset.dispoal.business.casedispose.domain.db.mapper.CasedisposeMapper;
import com.asset.dispoal.business.casedispose.domain.dto.CaseDisposeState;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import com.asset.dispoal.file.casedispose.dto.FileCaseConditionRequest;
import com.asset.dispoal.file.casedispose.dto.FileCaseJudgmentRequest;
import com.asset.dispoal.file.casedispose.service.command.FileCaseJudgmentCommand;
import com.asset.dispoal.file.casedispose.strategy.BaseFileUploadConditionStrategyServer;
import com.asset.dispoal.file.casedispose.strategy.dto.JudgmentExcelUploadDTO;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.strategy.StrategyHandlerParam;
import tech.mhuang.pacebox.core.strategy.StrategyHandlerResult;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.core.util.ObjectUtil;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class CaseJudgmentStrategyServer implements BaseFileUploadConditionStrategyServer {
    @Autowired
    private CasedisposeMapper casedisposeMapper;

    @Autowired
    private FileCaseJudgmentCommand fileCaseJudgmentCommand;

    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public StrategyHandlerResult<Result> strategy(StrategyHandlerParam<FileCaseConditionRequest> request) {
        List<CaseDisposeState> caseStateList = casedisposeMapper.queryCaseStateByAppointId(request.getData().getAppointId());
        if (CollectionUtil.isEmpty(caseStateList)) {
            return new StrategyHandlerResult<>(false, Result.faild("批次包的案件不存在"));
        }
        caseStateList = caseStateList.stream().filter(caseState -> AgreementCaseSateMenu.REGISTER_CASE.getLabel().equals(caseState.getState())).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(caseStateList)) {
            return new StrategyHandlerResult<>(false, Result.faild("批次包下的案件没有待立案的案件"));
        }
        if (StringUtil.equals(request.getData().getType(), "2")) {
            List<JudgmentExcelUploadDTO> idCardList = EasyExcel.read(request.getData().getFile().getInputStream()).head(JudgmentExcelUploadDTO.class).sheet().doReadSync();
            if (CollectionUtil.isEmpty(idCardList)) {
                return new StrategyHandlerResult<>(false, Result.faild("身份证号不存在"));
            }
            Map<String, JudgmentExcelUploadDTO> judgmentExcelUploadDTOIdCardMap = idCardList.stream().collect(Collectors.toMap(JudgmentExcelUploadDTO::getIdCard, dto -> dto));
            Map<String, JudgmentExcelUploadDTO> judgmentExcelUploadDTOLitigationNumberMap = idCardList.stream().collect(Collectors.toMap(JudgmentExcelUploadDTO::getLitigationNumber, dto -> dto));
            boolean checked = Boolean.TRUE;
            for (CaseDisposeState caseDisposeState : caseStateList) {
                JudgmentExcelUploadDTO dto = judgmentExcelUploadDTOIdCardMap.get(caseDisposeState.getIdCard());
                if (ObjectUtil.isEmpty(dto)) {
                    dto = judgmentExcelUploadDTOLitigationNumberMap.get(caseDisposeState.getLitigationNumber());
                }
                if (ObjectUtil.isNotEmpty(dto)) {
                    checked = false;
                    FileCaseJudgmentRequest fileCaseJudgmentRequest = new FileCaseJudgmentRequest();
                    fileCaseJudgmentRequest.setId(caseDisposeState.getId());
                    fileCaseJudgmentRequest.setJudgmentTime(dto.getJudgmentTime());
                    fileCaseJudgmentRequest.setPrincipal(dto.getPrincipal());
                    fileCaseJudgmentRequest.setInterest(dto.getInterest());
                    fileCaseJudgmentRequest.setPenaltyInterest(dto.getPenaltyInterest());
                    fileCaseJudgmentRequest.setCaseAcceptAmount(dto.getCaseAcceptAmount());
                    fileCaseJudgmentRequest.setJudgmentPreservationAmount(dto.getJudgmentPreservationAmount());
                    fileCaseJudgmentRequest.setBaseNumber1(dto.getBaseNumber1());
                    fileCaseJudgmentRequest.setBaseNumber2(dto.getBaseNumber2());
                    fileCaseJudgmentRequest.setBaseNumber3(dto.getBaseNumber3());
                    fileCaseJudgmentRequest.setBaseNumber4(dto.getBaseNumber4());
                    fileCaseJudgmentRequest.setBaseNumber5(dto.getBaseNumber5());
                    fileCaseJudgmentRequest.setJudgmentType1(dto.getJudgmentType1());
                    fileCaseJudgmentRequest.setJudgmentType2(dto.getJudgmentType2());
                    fileCaseJudgmentRequest.setJudgmentType3(dto.getJudgmentType3());
                    fileCaseJudgmentRequest.setJudgmentType4(dto.getJudgmentType4());
                    fileCaseJudgmentRequest.setJudgmentType5(dto.getJudgmentType5());
                    fileCaseJudgmentRequest.setJudgmentRate1(dto.getJudgmentRate1());
                    fileCaseJudgmentRequest.setJudgmentRate2(dto.getJudgmentRate2());
                    fileCaseJudgmentRequest.setJudgmentRate3(dto.getJudgmentRate3());
                    fileCaseJudgmentRequest.setJudgmentRate4(dto.getJudgmentRate4());
                    fileCaseJudgmentRequest.setJudgmentRate5(dto.getJudgmentRate5());
                    fileCaseJudgmentRequest.setJudgmentCalculationTime1(dto.getJudgmentCalculationTime1());
                    fileCaseJudgmentRequest.setJudgmentCalculationTime2(dto.getJudgmentCalculationTime2());
                    fileCaseJudgmentRequest.setJudgmentCalculationTime3(dto.getJudgmentCalculationTime3());
                    fileCaseJudgmentRequest.setJudgmentCalculationTime4(dto.getJudgmentCalculationTime4());
                    fileCaseJudgmentRequest.setJudgmentCalculationTime5(dto.getJudgmentCalculationTime5());
                    fileCaseJudgmentRequest.setJudgmentDay1(dto.getJudgmentDay1());
                    fileCaseJudgmentRequest.setJudgmentDay2(dto.getJudgmentDay2());
                    fileCaseJudgmentRequest.setJudgmentDay3(dto.getJudgmentDay3());
                    fileCaseJudgmentRequest.setJudgmentDay4(dto.getJudgmentDay4());
                    fileCaseJudgmentRequest.setJudgmentDay5(dto.getJudgmentDay5());
                    fileCaseJudgmentRequest.setBrief(dto.getBrief());
                    fileCaseJudgmentRequest.setJudge(dto.getJudge());
                    fileCaseJudgmentCommand.execute(fileCaseJudgmentRequest);
                }
            }
            if (checked) {
                return new StrategyHandlerResult<>(false, Result.faild("找不到导入条件的身份证号"));
            }
        } else {
            for (CaseDisposeState caseDisposeState : caseStateList) {
                FileCaseJudgmentRequest fileCaseJudgmentRequest = new FileCaseJudgmentRequest();
                fileCaseJudgmentRequest.setId(caseDisposeState.getId());
                fileCaseJudgmentCommand.execute(fileCaseJudgmentRequest);
            }
        }
        return new StrategyHandlerResult<>(true, Result.ok());
    }

    @Override
    public List<String> event() {
        return Collections.singletonList("3");
    }
}
